Previous Book Contents Book Index Next

Inside Macintosh: Overview /
Chapter 8 - Menus


Handling Keyboard Equivalents

Keyboard equivalents of menu commands allow the user to invoke a menu command from the keyboard. You can determine if the user chose the keyboard equivalent of a menu command by examining the event record for a key-down event. If the user pressed the Command key in combination with another character, you can then determine if this combination maps to a known Command-key equivalent by calling the Menu Manager function MenuKey. Listing 8-5 shows the Venn Diagrammer application's DoKeyDown procedure, which handles key-down events and determines if a keyboard equivalent was pressed.

Listing 8-5 Handling Command-key equivalents

PROCEDURE DoKeyDown (myEvent: EventRecord);
   VAR
      myKey:      char;
BEGIN
   myKey := chr(BAnd(myEvent.message, charCodeMask));
   IF (BAnd(myEvent.modifiers, CmdKey) <> 0) THEN
      BEGIN
         DoMenuAdjust;
         DoMenuCommand(MenuKey(myKey));
      END;
END;
The DoKeyDown procedure first extracts the pressed key from the message field of the event record and then examines the modifiers field to determine whether the Command key was also pressed. If so, the application first adjusts its menus and then calls the DoMenuCommand procedure defined in Listing 8-3 on page 157. In turn, DoKeyDown passes to DoMenuCommand the value returned from the MenuKey function. If the key combination pressed by the user is not the keyboard equivalent of any currently enabled menu item, then MenuKey sets the high-order word of its return value to 0.

Note
The Venn Diagrammer application does not accept any text input from the user. As a result, the DoKeyDown procedure shown in Listing 8-5 doesn't need an ELSE clause to handle keypresses in which the Command key is not held down.
Several keyboard equivalents (listed in Table 8-1) are reserved for common commands in the File and Edit menus. If your application supports these commands, you should assign these equivalents to the specified commands. Otherwise, you should ignore these keyboard equivalents.

Table 8-1 Table 8-1 Reserved keyboard equivalents
KeysCommandMenu
 

graphics/Command-key_Symbol_Prop.jpg

-A

Select AllEdit
 

graphics/Command-key_Symbol_Prop.jpg

-C

CopyEdit
 

graphics/Command-key_Symbol_Prop.jpg

-N

NewFile
 

graphics/Command-key_Symbol_Prop.jpg

-O

Open...File
 

graphics/Command-key_Symbol_Prop.jpg

-P

Print...File
 

graphics/Command-key_Symbol_Prop.jpg

-Q

QuitFile
 

graphics/Command-key_Symbol_Prop.jpg

-S

SaveFile
 

graphics/Command-key_Symbol_Prop.jpg

-V

PasteEdit
 

graphics/Command-key_Symbol_Prop.jpg

-W

CloseFile
 

graphics/Command-key_Symbol_Prop.jpg

-X

CutEdit
 

graphics/Command-key_Symbol_Prop.jpg

-Z

UndoEdit

IMPORTANT
You should never assign the keyboard equivalents listed in Table 8-1 to other menu commands. This helps ensure predictable behavior among all applications.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
9 JUL 1996